package com.deng.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deng.api.entity.CloudChannel;
import com.deng.api.mapper.CloudChannelMapper;
import com.deng.api.service.ICloudChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deng.api.vo.PageVO;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author deng
 * @since 2022-08-10
 */
@Service
public class CloudChannelServiceImpl extends ServiceImpl<CloudChannelMapper, CloudChannel> implements ICloudChannelService {
    @Resource
    private CloudChannelMapper cloudChannelMapper;

    @Override
    public PageVO getAll(Integer page, Integer size, CloudChannel channel) {
        Page<CloudChannel> page1 = new Page<>(page,size);
        LambdaQueryWrapper<CloudChannel> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(channel.getName())){
            wrapper.eq(CloudChannel::getName,channel.getName());
        }
        if (channel.getType() != null && channel.getType() != 0){
            wrapper.eq(CloudChannel::getType,channel.getType());
        }
        wrapper.eq(CloudChannel::getChannelId,channel.getChannelId());
        return PageVO.toPageVO(cloudChannelMapper.selectPage(page1,wrapper));
    }

    @Override
    public CloudChannel getFirstChannel() {
        LambdaQueryWrapper<CloudChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CloudChannel::getChannelId, 0);
        wrapper.last("limit 1");
        return cloudChannelMapper.selectOne(wrapper);
    }
}
